package com.ruoyi.ui.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ui.domain.UiAttr;
import com.ruoyi.ui.domain.vo.UiAttrVo;
import com.ruoyi.ui.domain.vo.UiCategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.ui.domain.UiCategory;
import com.ruoyi.ui.service.IUiCategoryService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 分类Controller
 * 
 * @author ruoyi
 * @date 2022-09-06
 */
@RestController
@RequestMapping("/category")
public class UiCategoryController extends BaseController
{
    @Autowired
    private IUiCategoryService uiCategoryService;

    /**
     * 查询分类列表
     */
    @RequiresPermissions("ui:category:list")
    @GetMapping("/list")
    public TableDataInfo list(UiCategoryVo uiCategory)
    {
        startPage();
        List<UiCategoryVo> list = uiCategoryService.selectUiCategoryList(uiCategory);
        return getDataTable(list);
    }
    @RequiresPermissions("ui:category:lista")
    @GetMapping("/lista")
    public TableDataInfo list(UiAttrVo uiAttr)
    {
        startPage();
        List<UiAttrVo> list = uiCategoryService.selectUiAttrList(uiAttr);

        return getDataTable(list);
    }

    /**
     * 导出分类列表
     */
    @RequiresPermissions("ui:category:export")
    @Log(title = "分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UiCategoryVo uiCategory)
    {
        List<UiCategoryVo> list = uiCategoryService.selectUiCategoryList(uiCategory);
        ExcelUtil<UiCategoryVo> util = new ExcelUtil<UiCategoryVo>(UiCategoryVo.class);
        util.exportExcel(response, list, "分类数据");
    }

    /**
     * 获取分类详细信息
     */
    @RequiresPermissions("ui:category:query")
    @GetMapping(value = "/{categoryId}")
    public AjaxResult getInfo(@PathVariable("categoryId") Long categoryId)
    {
        return AjaxResult.success(uiCategoryService.selectUiCategoryByCategoryId(categoryId));
    }

    /**
     * 新增分类
     */
    @RequiresPermissions("ui:category:add")
    @Log(title = "分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UiCategory uiCategory)
    {
        return toAjax(uiCategoryService.insertUiCategory(uiCategory));
    }

    /**
     * 修改分类
     */
    @RequiresPermissions("ui:category:edit")
    @Log(title = "分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UiCategory uiCategory)
    {
        return toAjax(uiCategoryService.updateUiCategory(uiCategory));
    }

    /**
     * 删除分类
     */
    @RequiresPermissions("ui:category:remove")
    @Log(title = "分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{categoryIds}")
    public AjaxResult remove(@PathVariable Long[] categoryIds)
    {
        return toAjax(uiCategoryService.deleteUiCategoryByCategoryIds(categoryIds));
    }
}
